using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectsPool
{
    private static ObjectsPool instance=new ObjectsPool();
    public static ObjectsPool Instance => instance;
    private ObjectsPool()
    {
        GameObject[] gameObjects = LoadManager.Instance.LoadAll<GameObject>("Prefabs");
        foreach (GameObject gameObject in gameObjects)
        {
            dicGo[gameObject.name] = gameObject;
            Debug.Log(gameObject.name);
        }
        EventMangger.afterLoadSceneEvent += Clear;        
    }
    public static class ObjectsName {
        public const string Candy = "Candy";
        public const string Explosion = "Explosion";
        public const string ExplosionBlue = "ExplosionBlue";
        public const string ExplosionYellow = "ExplosionYellow";
        public const string ExplosionRed = "ExplosionRed";
        public const string ExplosionOrange = "ExplosionOrange";
        public const string ExplosionPurple = "ExplosionPurple";
        public const string ExplosionGreen = "ExplosionGreen";
    }
    private Dictionary<string ,Stack<GameObject>> pools=new Dictionary<string , Stack<GameObject>>();
    private Dictionary<string,GameObject> dicGo=new Dictionary<string ,GameObject>();
    public void Push(GameObject go)
    {
        if (go != null)
        {
            if (!pools.ContainsKey(go.name)) pools.Add(go.name, new Stack<GameObject>());
            
            pools[go.name].Push(go);
            
            go.SetActive(false);
            
        }
    }
    public GameObject Pop(string name,Transform parent,Vector3 position,Quaternion qua)
    {
        GameObject go=null;
        if (pools.ContainsKey(name))
        {
            if (pools[name].Count > 0)
            {
                go = pools[name].Pop();
                
                go.transform.SetParent(parent);
                go.transform.position = position;
                go.transform.rotation = qua;
                go.SetActive(true);
            }
            else
            {
                if (dicGo.ContainsKey(name))
                {
                    go = GameObject.Instantiate(dicGo[name], position, qua, parent);
                    go.name = name;
                }
                    
               
            }
        }
        else
        {
            pools.Add(name, new Stack<GameObject>());
            if (dicGo.ContainsKey(name))
            {
                go = GameObject.Instantiate(dicGo[name], position, qua, parent);
                go.name=name;
            }
                
        }
        return go;
    }
    public void Clear()
    {
        pools=new Dictionary<string, Stack<GameObject>>();
        Debug.Log("clear");
    }
    public void Show()
    {
        Debug.Log("pools.count:"+pools.Count);
        foreach(var pool in pools)
        {
            Debug.Log(pool.Key);
        }
    }
}
